﻿using System;
using System.Collections.Generic;
using System.Linq;

using System.Data;
using Prodma.DataAccess.Database;
using Prodma.DataAccess.Interfaces.Models;
using Prodma.DataAccess.Models.ViewModels;
using Prodma.DataAccess.Repositories;
using Prodma.DataAccess.Helper.Enumerators;

namespace Prodma.DataAccess.DataService
{
    public class RoleService : IRoleRepository
    {

        private DataContext context = new DataContext();


        #region IRecordOperation<tb_Role,RoleVm> Members

        public IList<RoleVm> GetList()
        {
            DataContext context = new DataContext();
            using (context)
            {
                var list = (from role in context.tb_Role
                            select new RoleVm()
                            {
                                Id = role.Id,
                                Name = role.Name,
                                State = new StateVm() { Id = role.tb_State.Id, Name = role.tb_State.Name }
                            }).OrderBy(x => x.Name).ToList();

                return list;
            }
        }
        public void New(tb_Role entity)
        {
            DataContext context = new DataContext();
            using (context)
            {
                context.AddTotb_Role(entity);
                context.SaveChanges();
            }
        }
        public RoleVm Edit(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var edit = (from role in context.tb_Role
                            where role.Id == id
                            select new RoleVm()
                            {
                                Id = role.Id,
                                Name = role.Name,
                                State = new StateVm() { Id = role.tb_State.Id }
                            }).FirstOrDefault();

                return edit;
            }
        }
        public void Update(int id, tb_Role entity)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var edit = (from role in context.tb_Role
                            where role.Id == id
                            select role).FirstOrDefault();

                edit.Name = entity.Name;
                edit.tb_StateReference.EntityKey = entity.tb_StateReference.EntityKey;

                context.SaveChanges();
            }
        }
        public void Delete(int id)
        {
            if (id == 1)
            {
                return;
            }
            DataContext context = new DataContext();
            using (context)
            {

                var delete = (from role in context.tb_Role
                              where role.Id == id
                              select role).FirstOrDefault();

                context.DeleteObject(delete);
                context.SaveChanges();
            }
        }
        #endregion

        #region IFindOperation<RoleVm> Members

        public IList<RoleVm> GetList(Dictionary<string, string> find)
        {
            string findName = find["name"];
            DataContext context = new DataContext();
            using (context)
            {
                var list = (from role in context.tb_Role
                            where role.Name.StartsWith(findName)
                            select new RoleVm()
                            {
                                Id = role.Id,
                                Name = role.Name,
                                State = new StateVm() { Id = role.tb_State.Id, Name = role.tb_State.Name }
                            }).OrderBy(x => x.Name).ToList();

                return list;
            }
        }
        #endregion

        #region IStateOperation Members

        public void SetActive(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var state = (from role in context.tb_Role
                             where role.Id == id
                             select role).FirstOrDefault();

                state.tb_StateReference.EntityKey = new EntityKey("DataContext.tb_State", "Id", Convert.ToInt32(State.Active));

                context.SaveChanges();
            }
        }
        public void SetPassive(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var state = (from role in context.tb_Role
                             where role.Id == id
                             select role).FirstOrDefault();

                state.tb_StateReference.EntityKey = new EntityKey("DataContext.tb_State", "Id", Convert.ToInt32(State.Passive));

                context.SaveChanges();
            }
        }
        #endregion

        #region IManagerOperation<tb_Role> Members

        public void UpdateAuthorization(int roleId, int menuId, tb_Authorization authorization)
        {
            if (roleId == 1)
            {
                return;
            }
            DataContext context = new DataContext();
            using (context)
            {
                var delete = (from auth in context.tb_Authorization
                              where auth.tb_Role.Id == roleId &&
                                    auth.tb_MenuAdmin.Id == menuId
                              select auth).FirstOrDefault();
                if (delete != null)
                {
                    context.DeleteObject(delete);
                }

                context.AddTotb_Authorization(authorization);

                context.SaveChanges();
            }
        }
        #endregion


    }
}
